import requests
import time
import datetime
import hashlib
import json


class refundApi:
    def __init__(self, date, timestamp, md5, sn, account, password, role_login, refund_password):
        self.headers = None
        self.date = date
        self.timestamp = timestamp
        self.md5 = md5
        self.sn = sn
        self.account = account
        self.password = password
        self.role_login = role_login
        self.refund_password = refund_password
        self.config_r2 = None
        self.token = None
        self.uid = None
        self.auth_token = None
        self.amount = None
        self.order_no = None
        self.access_token = None

    def config(self):
        # 拉取配置，获取配置列表 self.config_r2
        config_url = "http://open.dairphone.cn/device/config_list"
        self.headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        config_data = {
            'check_code': self.md5,
            'time': self.timestamp,
            'sn': self.sn,
            'version': '3.3.2'
        }
        config_r1 = requests.post(config_url, data=config_data, headers=self.headers)
        self.config_r2 = config_r1.json()
        print(json.dumps(self.config_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))

    def status(self):
        # 请求激活接口，获取self.token，self.uid等
        status_url = self.config_r2['data']['activation']['url']
        print(status_url)
        sign = self.config_r2['data']['activation']['param_mapping']['sign']
        # print(sign)
        status_data = {
            "sn": self.sn,
            sign: self.md5,
            "time": self.timestamp,
            "version": "3.3.2"
        }
        status_r1 = requests.post(status_url, data=status_data, headers=self.headers)
        status_r2 = status_r1.json()
        print(json.dumps(status_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        self.token = status_r2['data']['token']
        self.uid = status_r2['data']['uid']
        # print(self.token)
        # print(self.uid)

    def login(self):
        # 商户/员工登录（独立收银）,获取self.auth_token
        face_login_url = self.config_r2['data']['face_login']['url']
        print(face_login_url)
        face_login_data = {
            "sn": self.sn,
            "account": self.account,
            "password": self.password,
            "role_login": self.role_login,
            "token": self.token
        }
        face_login_r1 = requests.post(face_login_url, data=face_login_data, headers=self.headers)
        face_login_r2 = face_login_r1.json()
        print(json.dumps(face_login_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        self.auth_token = face_login_r2['data']['auth_token']
        # print(self.auth_token)

    def order_list(self):
        # 收款记录列表，获取self.amount，self.order_no
        refund_order_list_url = self.config_r2['data']['refund_order_list']['url']
        # print(refund_order_list_url)
        refund_order_list_data = {
            "sn": self.sn,
            "auth_token": self.auth_token,
            "page": 1,
            "date": self.date,
            "token": self.token
        }
        refund_order_list_r1 = requests.post(refund_order_list_url, data=refund_order_list_data, headers=self.headers)
        refund_order_list_r2 = refund_order_list_r1.json()
        self.amount = refund_order_list_r2['data']['list'][0]['pay_price']
        self.order_no = refund_order_list_r2['data']['list'][0]['id']
        print(json.dumps(refund_order_list_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        # print(self.amount)
        # print(self.order_no)

    def refund(self):
        # 申请退款
        refund_url = self.config_r2['data']['refund']['url']
        print(refund_url)
        refund_data = {
            "sn": self.sn,
            "amount": self.amount,
            "order_no": self.order_no,
            "uid": self.uid,
            "password": self.refund_password,
            "auth_token": self.auth_token
        }
        refund_r1 = requests.post(refund_url, data=refund_data, headers=self.headers)
        refund_r2 = refund_r1.json()
        print(json.dumps(refund_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))

    def refund_query(self):
        # 退款查询
        refund_query_url = self.config_r2['data']['refund_query']['url']
        print(refund_query_url)
        refund_query_data = {
            "sn": self.sn,
            "order_no": self.order_no,
            "auth_token": self.auth_token
        }
        refund_query_r1 = requests.post(refund_query_url, data=refund_query_data, headers=self.headers)
        refund_query_r2 = refund_query_r1.json()
        print(json.dumps(refund_query_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))

    def refund_qrcode(self):
        # 退款二维码获取
        refund_qrcode_url = self.config_r2['data']['refund_qrcode']['url']
        print(refund_qrcode_url)
        refund_qrcode_data = {
            "sn": self.sn,
            "order_id": self.order_no,
            "uid": self.uid,
            "auth_token": self.auth_token
        }
        refund_qrcode_r1 = requests.post(refund_qrcode_url, data=refund_qrcode_data, headers=self.headers)
        refund_qrcode_r2 = refund_qrcode_r1.json()
        print(json.dumps(refund_qrcode_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        self.access_token = refund_qrcode_r2['data']['access_token']
        # print(self.access_token)

    def token_auth(self):
        # 刷脸设备token授权，获取self.openid
        token_auth_url = self.config_r2['data']['token_auth']['url']
        print(token_auth_url)
        token_auth_data = {
            "sn": self.sn,
            "access_token": self.access_token,  # refund_qrcode中返回的access_token
            "auth_token": self.auth_token  # login中返回的auth_token
        }
        token_auth_r1 = requests.post(token_auth_url, data=token_auth_data, headers=self.headers)
        token_auth_r2 = token_auth_r1.json()
        print(json.dumps(token_auth_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        # self.openid = token_auth_r2['openid']
        # print(self.openid)

    def newest_order(self):
        # 获取当前消费者最新订单
        newest_order_url = self.config_r2['data']['newest_order']['url']
        print(newest_order_url)
        newest_order_data = {
            "sn": self.sn,
            "openid": "ohTKdwI2_Sl7nUT6gRGyLGnCCpic",
            "auth_token": self.token
        }
        newest_order_r1 = requests.post(newest_order_url, data=newest_order_data, headers=self.headers)
        newest_order_r2 = newest_order_r1.json()
        print(json.dumps(newest_order_r2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))


date1 = datetime.datetime.now().strftime("%Y-%m-%d")
timestamp1 = round(time.time())
a = str(timestamp1) + 'CMJF_getSignInfo'
md5 = hashlib.md5()
md5.update(a.encode('utf-8'))
md51 = md5.hexdigest()
print(timestamp1)
print(md51)
sn1 = str(input("请输入要测试的SN(CMAC692000004957REP8YnS)："))  # CMAC692000004957REP8YnS
account1 = str(input("请输入商户/员工/店长/财务账号(17620533205)："))  # 17620533205
password1 = str(input("请输入商户/员工/店长/财务密码(123456b)："))  # 123456b
role_login1 = str(input("请输入0/1(商户0，员工1)："))  # 0
refund_password1 = str(input("请输入退款密码(123456)："))  # 123456
refundApi = refundApi(date1, timestamp1, md51, sn1, account1, password1, role_login1, refund_password1)
refundApi.config()
refundApi.status()
time.sleep(2)
refundApi.login()
time.sleep(10)
refundApi.order_list()
time.sleep(10)
refundApi.refund()
time.sleep(10)
refundApi.refund_query()
time.sleep(10)
refundApi.refund_qrcode()
time.sleep(10)
# refundApi.token_auth() 暂时写死openid
refundApi.newest_order()
time.sleep(20)
